package com.wd.live.module_wide

import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.tabs.TabLayout
import com.jcodecraeer.xrecyclerview.XRecyclerView

import com.wd.live.module_base.bean.FindLiveListBean
import com.wd.live.module_base.bean.FindLiveTypeList

import com.wd.live.module_base.ui.BaseFragment

import com.wd.live.module_wide.databinding.FragmentWideBinding
import com.wd.live.module_wide.view.adapter.FindLiveListAdapter
import com.wd.live.module_wide.viewmodel.WideViewModel


// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [WideFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
@Route(path = "/module_wide/wide")
class WideFragment : BaseFragment<WideViewModel, FragmentWideBinding>() {
    private var labs = mutableListOf<FindLiveListBean>()
    private var labsType = mutableListOf<FindLiveTypeList>()
    private var typeArr = mutableListOf<Int>()
    var liveType:Int=101
    var page:Int=1
    var size:Int=10
    val aa:Int=1
    val b:Int=1
    val c:Int=1
    val d:Int=2


    override fun initData() {
        dataBinding.wideSearchTop.setOnClickListener(View.OnClickListener {
            ARouter.getInstance().build("/module_wide/search")
                .navigation()
        })

        dataBinding.xzId.setOnClickListener(View.OnClickListener {
            ARouter.getInstance().build("/module_wide/signs")
                .navigation()
        })
        viewModel.getFindLiveTypeList()
        viewModel.dataFindtypeList.observe(this){
            it.result?.let {
                it1->labsType.addAll(it1)
            }
            labsType.forEach { findLiveTypeList ->
                dataBinding.wideTb.addTab(dataBinding.wideTb.newTab().setText(findLiveTypeList.name))
                typeArr.add(findLiveTypeList.id)
            }
            dataBinding.wideTb.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    if (tab!=null){
                        labs.clear()
                        liveType=typeArr[tab.position]
                        Log.i("TAG", "onTabSelected: "+liveType)
                        viewModel.getFindLiveList(liveType,page,size)
                    }
                }
                override fun onTabUnselected(tab: TabLayout.Tab?) {
                   tab?.customView=null
                }
            })
        }

        viewModel.getFindLiveList(liveType, page, size)

        viewModel.dataFindLiveList.observe(this){
            it.result?.let { it1 -> labs.addAll(it1)
            }
            dataBinding.wideXr?.adapter= FindLiveListAdapter(context,R.layout.live_list_item,labs)
        }

        val gridLayoutManager = GridLayoutManager(context, 2)
        dataBinding?.wideXr?.layoutManager=gridLayoutManager

        dataBinding.wideXr.setLoadingListener(object :XRecyclerView.LoadingListener{
            override fun onRefresh() {
                dataBinding.wideXr.reset()
                if (page!=1){
                    page=1
                }
                viewModel.getFindLiveList(liveType, page, size)
            }

            override fun onLoadMore() {
                dataBinding.wideXr.reset()
                page++
                viewModel.getFindLiveList(liveType, page, size)
            }
        })
    }

    override fun getViewModelClass(): Class<WideViewModel> {
        return WideViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.fragment_wide
    }


}